/**
 * 应用服务
 * 处理应用相关的IPC通信
 */
import { BaseIPCService, ServiceResult, createSuccessResult, createErrorResult } from './base/ipc.service'

/**
 * 应用信息接口
 */
export interface AppInfo {
  version: string
  name: string
  appPath: string
  locale: string
  isPackaged: boolean
  platform: string
  arch: string
  osVersion: string
  osName: string
}

/**
 * 应用服务类
 */
export class AppService extends BaseIPCService {
  /**
   * 获取应用版本
   */
  async getAppVersion(): Promise<ServiceResult<string>> {
    try {
      this.log('获取应用版本')

      const result = await this.invoke<string>('app:version')
      
      this.log('获取应用版本成功', { version: result })
      return createSuccessResult(result, '获取应用版本成功')

    } catch (error) {
      this.logError('获取应用版本', error)
      return createErrorResult(error, '获取应用版本失败')
    }
  }

  /**
   * 获取应用信息
   */
  async getAppInfo(): Promise<ServiceResult<AppInfo>> {
    try {
      this.log('获取应用信息')

      const result = await this.invoke<AppInfo>('get-app-info')
      
      this.log('获取应用信息成功', { version: result.version, platform: result.platform })
      return createSuccessResult(result, '获取应用信息成功')

    } catch (error) {
      this.logError('获取应用信息', error)
      return createErrorResult(error, '获取应用信息失败')
    }
  }

  /**
   * 退出应用
   */
  async quitApp(): Promise<ServiceResult<void>> {
    try {
      this.log('退出应用')

      await this.invoke<void>('app:quit')
      
      this.log('退出应用成功')
      return createSuccessResult(undefined, '应用已退出')

    } catch (error) {
      this.logError('退出应用', error)
      return createErrorResult(error, '退出应用失败')
    }
  }

  /**
   * 重启应用
   */
  async restartApp(): Promise<ServiceResult<void>> {
    try {
      this.log('重启应用')

      await this.invoke<void>('app:restart')
      
      this.log('重启应用成功')
      return createSuccessResult(undefined, '应用正在重启')

    } catch (error) {
      this.logError('重启应用', error)
      return createErrorResult(error, '重启应用失败')
    }
  }

  /**
   * 最小化窗口
   */
  async minimizeWindow(): Promise<ServiceResult<void>> {
    try {
      this.log('最小化窗口')

      await this.invoke<void>('app:minimize')
      
      this.log('最小化窗口成功')
      return createSuccessResult(undefined, '窗口已最小化')

    } catch (error) {
      this.logError('最小化窗口', error)
      return createErrorResult(error, '最小化窗口失败')
    }
  }

  /**
   * 最大化/还原窗口
   */
  async maximizeWindow(): Promise<ServiceResult<void>> {
    try {
      this.log('最大化/还原窗口')

      await this.invoke<void>('app:maximize')
      
      this.log('最大化/还原窗口成功')
      return createSuccessResult(undefined, '窗口状态已切换')

    } catch (error) {
      this.logError('最大化/还原窗口', error)
      return createErrorResult(error, '切换窗口状态失败')
    }
  }

  /**
   * 关闭窗口
   */
  async closeWindow(): Promise<ServiceResult<void>> {
    try {
      this.log('关闭窗口')

      await this.invoke<void>('app:close')
      
      this.log('关闭窗口成功')
      return createSuccessResult(undefined, '窗口已关闭')

    } catch (error) {
      this.logError('关闭窗口', error)
      return createErrorResult(error, '关闭窗口失败')
    }
  }

  /**
   * 打开外部链接
   */
  async openExternalLink(url: string): Promise<ServiceResult<void>> {
    try {
      this.validateRequired({ url }, ['url'])
      
      // 简单的URL验证
      if (!url.startsWith('http://') && !url.startsWith('https://')) {
        throw new Error('无效的URL格式')
      }

      this.log('打开外部链接', { url })

      await this.invoke<void>('open-external-link', url)
      
      this.log('打开外部链接成功', { url })
      return createSuccessResult(undefined, '外部链接已打开')

    } catch (error) {
      this.logError('打开外部链接', error, { url })
      return createErrorResult(error, '打开外部链接失败')
    }
  }

  /**
   * 显示文件选择对话框
   */
  async showOpenDialog(options: {
    type?: 'file' | 'directory'
    filters?: Array<{ name: string; extensions: string[] }>
  } = {}): Promise<ServiceResult<{ paths: string[]; canceled: boolean }>> {
    try {
      this.log('显示文件选择对话框', options)

      const result = await this.invoke<{ paths: string[]; canceled: boolean }>('system:openDialog', options)
      
      this.log('文件选择对话框结果', { canceled: result.canceled, pathCount: result.paths.length })
      return createSuccessResult(result, result.canceled ? '用户取消选择' : '文件选择成功')

    } catch (error) {
      this.logError('显示文件选择对话框', error, options)
      return createErrorResult(error, '显示文件选择对话框失败')
    }
  }

  /**
   * 显示保存对话框
   */
  async showSaveDialog(options: {
    defaultPath?: string
    filters?: Array<{ name: string; extensions: string[] }>
  } = {}): Promise<ServiceResult<{ path?: string; canceled: boolean }>> {
    try {
      this.log('显示保存对话框', options)

      const result = await this.invoke<{ path?: string; canceled: boolean }>('system:showSaveDialog', options)
      
      this.log('保存对话框结果', { canceled: result.canceled, path: result.path })
      return createSuccessResult(result, result.canceled ? '用户取消保存' : '保存路径选择成功')

    } catch (error) {
      this.logError('显示保存对话框', error, options)
      return createErrorResult(error, '显示保存对话框失败')
    }
  }
}

// 导出单例实例
export const appService = new AppService()

// 导出便捷方法
export const {
  getAppVersion,
  getAppInfo,
  quitApp,
  restartApp,
  minimizeWindow,
  maximizeWindow,
  closeWindow,
  openExternalLink,
  showOpenDialog,
  showSaveDialog
} = appService
